using System;
using UnityEngine;
using QFramework;
using Sirenix.OdinInspector;

namespace Survivor
{
	public partial class Axe : ViewController
	{
		[HideInInspector] private Vector2 _initSpeed;
		[HideInInspector] public Vector2 speed;
		[HideInInspector] public float gravity;  // 重力加速度
		private Vector3 spinSpeed = new Vector3(0, 0, 30); // 动画的旋转速度，与游戏逻辑无关
		public float aliveTime;
		void Start()
		{
			// Code Here
			ActionKit.Delay(aliveTime, () =>
			{
				gameObject.DestroySelfGracefully();
			}).Start(this);
		}

		public void Init(Vector2 position,Vector2 initSpeed,float gravity)
		{
			_initSpeed = initSpeed;
			transform.position = position;
			speed = _initSpeed;
			this.gravity = gravity;
			if (initSpeed.x > 0) transform.rotation = Quaternion.Euler(0, 180, 0);
		}

		
		public void Update()
		{
			speed.y -= gravity * Time.deltaTime;
			transform.position += new Vector3(speed.x * Time.deltaTime,speed.y * Time.deltaTime);
			transform.Rotate(spinSpeed * Time.deltaTime);
		}

		private void OnTriggerEnter2D(Collider2D other)
		{
			var enemy = other.gameObject.GetComponentInParent<Enemy>();
			if (enemy)
			{
				enemy.Hurt(Global.SimpleAxeDamage.Value);
			}
		}
	}
}
